package com.yikaiye.android.yikaiye.ui.activity.mine;

import androidx.annotation.NonNull;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.base.BaseActivity;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.databinding.ActivityMessageBinding;
import com.yikaiye.android.yikaiye.ui.activity.mine.viewmodel.MessageListViewModel;
import com.yikaiye.android.yikaiye.ui.adapter.MessageListAdapter;
import com.yikaiye.android.yikaiye.utils.ToastHelper;
import com.yikaiye.android.yikaiye.utils.UMengUtil;

/**
 * 消息
 */
public class MessageActivity extends BaseActivity<ActivityMessageBinding, MessageListViewModel> {

    private MessageListAdapter adapter;
    private boolean isFirstRefresh = true;
    private int page = 1;

    @Override
    protected MessageListViewModel bindModel() {
        return getViewModel(this, MessageListViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void initData() {
        initTitleBar(getString(R.string.message_text));
        UMengUtil.setCountEventId(Constant.MINE_PAGE_MY_MESSAGE);
//        binding.smartRefreshLayout.setEnableOverScrollDrag(true);
        adapter = new MessageListAdapter(context);
        binding.recyclerView.setAdapter(adapter);
        adapter.setEmptyViewEnable(true);
        adapter.setEmptyViewLayout(context, R.layout.layout_empty_list);
        viewModel.getMessageList(page);
    }

    @Override
    protected void initClick() {
        binding.smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                viewModel.getMessageList(page);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                viewModel.getMessageList(page);
            }
        });
        // 消息列表
        viewModel.messageListMutableLiveData.observe(this, messageModels -> {
            if (page == 1) {
                adapter.submitList(messageModels);
                binding.smartRefreshLayout.finishRefresh();
            } else {
                adapter.addAll(messageModels);
                binding.smartRefreshLayout.finishLoadMore();
            }
            if (isFirstRefresh) {
                viewModel.allReadMessage();
            }
        });
        viewModel.mErrorData.observe(this, ToastHelper::show);

        // 全部已读成功
        viewModel.readSuccessMutableLiveData.observe(this, s -> isFirstRefresh = false);

        // 删除成功
        viewModel.delSuccessMutableLiveData.observe(this, s -> {
            page = 1;
            binding.recyclerView.scrollToPosition(0);
            viewModel.getMessageList(page);
        });
        viewModel.delErrorMutableLiveData.observe(this, ToastHelper::show);

        adapter.setOnChildItemClickListener(id -> {
            UMengUtil.setCountEventId(Constant.MINE_PAGE_MESSAGE_CLICK_DELETE);
            viewModel.deleteMessage(id);
        });
    }
}